#splines.py

import math
import wx

#Generates a set of points along a given spline
def generateSplinePoints(pt1, pt2, pt3, pt4, bias, tension, numpts):
    delmu = 1.0/numpts
    (x1, y1) = pt1
    (x2, y2) = pt2
    (x3, y3) = pt3
    (x4, y4) = pt4
    pts = list()
    for i in range(0, numpts+1):
        mu = delmu*i
        npt = (interpolateSpline(x1, x2, x3, x4, bias, tension, mu), 
              interpolateSpline(y1, y2, y3, y4, bias, tension, mu))
        pts.append(npt)
    return pts

#Interpolates along a given cubic hermite spline
#mu = [0,1]
def interpolateSpline(x1, x2, x3, x4, bias, tension, mu):
    mu2 = mu * mu		#mu^2
    mu3 = mu2 * mu		#mu^3
    #m0 = averaged slope of x1->x3
    m0 = (x2 - x1)*(1.0 + bias)*(1.0 - tension) / 2.0
    m0 += (x3 - x2)*(1.0 - bias)*(1.0 - tension) / 2.0
    #m1 = averaged slope of x2->x4
    m1 = (x3 - x2)*(1.0 + bias)*(1.0 - tension) / 2.0
    m1 += (x4 - x3)*(1.0 - bias)*(1.0 - tension) / 2.0
    
    #h_00(mu) first hermite basis function
    h00 = 2.0 * mu3 - 3.0 * mu2 + 1.0
    #h_10(mu) second hermite basis function
    h10 = mu3 - 2.0 * mu2 + mu
    #h_01(mu) third hermite basis function
    h01 = -2.0 * mu3 + 3.0 * mu2
    #h_11(mu) fourth hermite basis function
    h11 = mu3 - mu2
    
    retval = h00*x2 + h10*m0 + h11*m1 + h01*x3
    return retval
